import Vue from 'vue'
import Vuex from 'vuex'
import {
	login,
	
} from "@/api/login.js"
import config from '@/utils/config.js'
Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
		token: uni.getStorageSync("token"),
		userInfo: uni.getStorageSync("userInfo") || {},
		systemInfo: {},
		locateInformation: {}
	},
	mutations: {
		update(state, [key, value, storage = false]) {
			state[key] = value;
			if (storage) {
				uni.setStorageSync(key, value)
			}
		},
		setSystemInfo(state, data) {
			state.systemInfo = Object.assign({}, state.systemInfo, data);
		},
		setLocateInformation(state, data) {
			state.locateInformation = data
		},
	},
	actions: {
		// 微信登录
		Login({
			commit
		}, data) {
			return new Promise((resolve, reject) => {
				login(data)
					.then(res => {
						commit('update', ['token', res.data.access_token, true])
						resolve()
					})
					.catch(error => {
						reject(error)
					})
			})
		},

		// 获取用户信息
		GetInfo({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				getInfo({
						userId: state.userId
					})
					.then(res => {
						const user = res.data
						user.avatar = user.avatar == '' ?
							'https://www.banlift.com/oss/app/avatar.png' :
							user.avatar
						commit('update', ["userInfo", user, true])
						resolve(res)
					})
					.catch(error => {
						reject(error)
					})
			})
		},
		// 退出系统
		LogOut({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				uni.clearStorageSync()
				resolve()
				// logout(state.token)
				// 	.then(() => {
				// 		uni.clearStorageSync()
				// 		resolve()
				// 	})
				// 	.catch(error => {
				// 		reject(error)
				// 	})
			})
		},
	}
})
export default store